package com.zxy.lm.controller;

import com.zxy.lm.DO.BorrowRecordDO;
import com.zxy.lm.entity.Announcement;
import com.zxy.lm.entity.Book;
import com.zxy.lm.entity.Type;
import com.zxy.lm.entity.User;
import com.zxy.lm.service.AnnouncementService;
import com.zxy.lm.service.BookService;
import com.zxy.lm.service.BorrowRecordService;
import com.zxy.lm.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PageController
 * @Author 14995
 * @Date 2021-6-22 19:06
 * @Version 1.0
 */
@Controller
public class PageController {

    @Autowired
    private BookService bookService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private BorrowRecordService borrowRecordService;

    @Autowired
    private AnnouncementService announcementService;

    @GetMapping("/main")
    public String main() {
        return "main";
    }

    @GetMapping("/queryBook")
    public String queryBook(Model model) {
        List<Book> list = bookService.queryAllBook();
        Map<Integer, String> map = typeService.queryMapAllType();
        model.addAttribute("bookList", list);
        model.addAttribute("typeMap", map);
        return "queryBook";
    }

    @GetMapping("/nav")
    public String nav() {
        return "nav";
    }

    @GetMapping("/navLeft")
    public String navLeft() {
        return "navLeft";
    }

    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @GetMapping("/announcement")
    public String announcement(Model model) {
        List<Announcement> announcementList = announcementService.getAllAnnouncement();
        model.addAttribute("announcementList", announcementList);
        return "announcement";
    }

    @GetMapping("/record")
    public String record(HttpSession session, Model model) {
        Object o = session.getAttribute("user");
        if (!(o instanceof User)){
            model.addAttribute("lmsg", "请先登录");
            return "index";
        }
        User user = (User) o;
        List<BorrowRecordDO> list = borrowRecordService.getAllRecordDOByUserId(user.getId());
        model.addAttribute("recordDOList", list);
        return "record";
    }

    @GetMapping("/info")
    public String info() {
        return "info";
    }

    @GetMapping("/changePassword")
    public String changePassword() {
        return "changePassword";
    }
}
